package com.ce.pms.system.occupation.service.impl;

import java.util.List;
import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.charge.dao.IParameterDefDao;
import com.ce.pms.charge.model.BusinessCodeDomain;
import com.ce.pms.charge.model.ParameterDefDomain;
import com.ce.pms.charge.service.IBusinessCodeService;
import com.ce.pms.charge.vo.BusinessCodeVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.service.impl.AbstractCustomerEventServiceImpl;
import com.ce.pms.system.occupation.dao.IOccupationChargeItemConfigDao;
import com.ce.pms.system.occupation.model.OccupationChargeItemConfigDO;
import com.ce.pms.system.occupation.service.IOccupationChargeItemConfigService;
import com.ce.pms.system.occupation.vo.OccupationChargeItemConfigVO;
import com.sitechasia.webx.core.support.Page;
/**
 * 功能描述: 入伙缴费设置Service实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-29
 * Edit author:
 * Edit on:
 * 修改原因：

 */
public class OccupationChargeItemConfigServiceImpl extends AbstractCustomerEventServiceImpl<OccupationChargeItemConfigVO,OccupationChargeItemConfigDO>
		implements IOccupationChargeItemConfigService {
	/**
	 * 方法描述: 增加入伙缴费设置方法
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param occupationChargeItemConfigVO
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public OccupationChargeItemConfigVO addPerfectItemTypeConfig(OccupationChargeItemConfigVO occupationChargeItemConfigVO)
	{
		if(BlankUtil.isBlank(occupationChargeItemConfigVO) || BlankUtil.isBlank(occupationChargeItemConfigVO.getPayItemName()))
		{
			return null;
		}
		checkExistenceByName(occupationChargeItemConfigVO.getPayItemName());
		OccupationChargeItemConfigDO occupationChargeItemConfigDO = assembleV2D(occupationChargeItemConfigVO,null);
		occupationChargeItemConfigDO = occupationChargeItemConfigDao.saveObject(occupationChargeItemConfigDO);
		occupationChargeItemConfigVO.setKeyword(occupationChargeItemConfigDO.getKeyword()) ;
		return occupationChargeItemConfigVO;
	}
	/**
	 * 方法描述: 验证入伙缴费设置收费项目名字是否存在
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param name
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkNameExisited(String name){
		if(isNameExisit(name)){
			throw new BusinessException(getNameExisitExceptionInoKey(),BusinessException.WARNING);
		}
	}
	/**
	 * 方法描述: 删除入伙缴费设置方法
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param occupationChargeItemConfigId
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean deleteOccupationChargeItemConfigById(Long occupationChargeItemConfigId)
	{
		if(occupationChargeItemConfigId==null)
		{
			return false;
		}
		try
		{
			occupationChargeItemConfigDao.deleteObjectById(occupationChargeItemConfigId);
			return true;
		}
		catch(RuntimeException e)
		{	//如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("occupation:system_delete_inused_exception",BusinessException.WARNING);
		}
	}
	/**
	 * 方法描述: 修改入伙缴费设置方法
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param occupationChargeItemConfigVO
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public OccupationChargeItemConfigVO editOccupationChargeItemConfig(OccupationChargeItemConfigVO occupationChargeItemConfigVO)
	{
		if(BlankUtil.isBlank(occupationChargeItemConfigVO) || BlankUtil.isBlank(occupationChargeItemConfigVO.getKeyword()))
		{
			return null;
		}
		OccupationChargeItemConfigDO occupationChargeItemConfigDO = occupationChargeItemConfigDao.findById(occupationChargeItemConfigVO.getKeyword());
		if(occupationChargeItemConfigDO==null)
		{
			return null;
		}
		if(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals((occupationChargeItemConfigVO.getMonthPayYn()))){
			occupationChargeItemConfigDO.setPayMonthCount(null);
		}
		modifyInfo(occupationChargeItemConfigDO.getSysInfo(), occupationChargeItemConfigVO.getModifier());
		occupationChargeItemConfigDO = super.assembleV2D(occupationChargeItemConfigVO, occupationChargeItemConfigDO);
		occupationChargeItemConfigDao.updateObject(occupationChargeItemConfigDO);
		return occupationChargeItemConfigVO;
	}
	/**
	 * 方法描述: 获取入伙物业预交款交易码及其他支付类型交易码方法
	 * @author 胡煦
	 * Create on:2009-1-16
	 * @param
	 * @return List<BusinessCodeVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<BusinessCodeVO> getBusinessCodeList(){
		// 查询入伙物业预交款交易码实例
		ParameterDefDomain parameterDefDomain = parameterDefDao.findUniqueBy(
				"pkey", "OCCUPATION_CHARGE_CODE");
		BusinessCodeVO businessCodeVO = businessCodeService.findBusinessCodeByKeyword(parameterDefDomain.getPvalue());
		List<BusinessCodeVO> list = businessCodeService.getAllIncomeBusinessCode();
		if(!BlankUtil.isBlank(list)){
			list.add(businessCodeVO);
		}
		return list;
	}
	/**
	 * 方法描述: 验证交易码集合中是否存在重复的
	 * @author 胡煦
	 * Create on:2009-1-19
	 * @param list，businessCodeVO
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private boolean checkHasCode(List<BusinessCodeVO> list ,BusinessCodeVO businessCodeVO){
		if(BlankUtil.isBlank(list)||BlankUtil.isBlank(businessCodeVO)){
			return false;
		}
		for(BusinessCodeVO businessCode : list ){
			if(businessCode.getBizCode().equals(businessCodeVO.getBizCode())){
				return false;
			}
		}
		return true;
	}
	/**
	 * 方法描述: 判断入伙缴费设置收费项目名字是否存在
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param name
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	protected boolean isNameExisit(String name){
		return occupationChargeItemConfigDao.findUniqueByName(name)!=null;
	}
	/**
	 * 方法描述: 分页查询入伙缴费设置方法
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param searchVO
	 * @return Page
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryOccupationChargeItemConfigListByPage(SearchVO searchVO)
	{
		return queryVOListByPage(searchVO);
	}



	/**
	 * 方法描述: 查询入伙缴费设置List方法
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param
	 * @return List<OccupationChargeItemConfigVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<OccupationChargeItemConfigVO> queryAllOccupationChargeItemConfig()
	{
		List<OccupationChargeItemConfigDO> occupationChargeItemConfigDOList = occupationChargeItemConfigDao.findAll();
		return assembleDOList2VOList(occupationChargeItemConfigDOList);
	}
	/**
	 * 方法描述: 根据ID查询入伙缴费设置VO方法
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param id
	 * @return OccupationChargeItemConfigVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public OccupationChargeItemConfigVO findOccupationChargeItemConfigVOById(Long id)
	{
		return super.findObjectVOById(id);
	}
	@Override
	protected OccupationChargeItemConfigDO findObjectDOById(Long id)
	{
		return occupationChargeItemConfigDao.findById(id);
	}


	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{
		return occupationChargeItemConfigDao.queryByPage(searchVO);
	}
	@Override
	/**
	 * 方法描述: DO to VO
	 * @author: 胡煦
	 * Create on: 2008-12-29
	 * @param sourceDO
	 * @param targetVO
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因:
	 */
	protected OccupationChargeItemConfigVO assembleD2V(OccupationChargeItemConfigDO sourceDO, OccupationChargeItemConfigVO targetVO) {
		OccupationChargeItemConfigVO configVO = assembleD2V(sourceDO, targetVO, null);
		configVO.setBusinessCodeId(sourceDO.getBusinessCode().getKeyword());
		configVO.setBusinessCodeName(sourceDO.getBusinessCode().getBizCode());
		return configVO;
	}
	@Override
	/**
	 * 方法描述: VO to DO
	 * @author: 胡煦
	 * Create on: 2008-12-29
	 * @param sourceDO
	 * @param targetVO
	 * @return
	 * Edit author:
	 * Edit on:
	 * 修改原因:
	 */
	protected OccupationChargeItemConfigDO assembleV2D(OccupationChargeItemConfigVO targetVO,OccupationChargeItemConfigDO sourceDO) {
		OccupationChargeItemConfigDO configDO = super.assembleV2D(targetVO, sourceDO);
		configDO.setBusinessCode(new BusinessCodeDomain(targetVO.getBusinessCodeId()));

		if(!BlankUtil.isBlank(targetVO.getCreator())){
			configDO.setSysInfo(createInfo(targetVO.getCreator()));
		}
		return configDO;
	}
	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "occupation:system_name_exist_exception";
	}
	private IOccupationChargeItemConfigDao occupationChargeItemConfigDao;
	private IBusinessCodeService businessCodeService;
	private IParameterDefDao parameterDefDao;
	/**
	 * @param occupationChargeItemConfigDao the occupationChargeItemConfigDao to set
	 */
	public void setOccupationChargeItemConfigDao(
			IOccupationChargeItemConfigDao occupationChargeItemConfigDao) {
		this.occupationChargeItemConfigDao = occupationChargeItemConfigDao;
	}
	/**
	 * @param businessCodeService the businessCodeService to set
	 */
	public void setBusinessCodeService(IBusinessCodeService businessCodeService) {
		this.businessCodeService = businessCodeService;
	}
	/**
	 * @param parameterDefDao the parameterDefDao to set
	 */
	public void setParameterDefDao(IParameterDefDao parameterDefDao) {
		this.parameterDefDao = parameterDefDao;
	}
}
